﻿Imports System.Windows.Forms
Imports NAppUpdate.Framework
Imports NAppUpdate.Framework.Common
Imports NAppUpdate.Framework.Sources


Public Class MDIParent1

    Private updatechecked As Boolean = False
    Dim updwindow As New updateform
    Dim localdir As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\" & System.Reflection.Assembly.GetExecutingAssembly.GetName.Name

    Private Sub ShowNewForm(ByVal sender As Object, ByVal e As EventArgs) Handles NewToolStripMenuItem.Click, NewToolStripButton.Click, NewWindowToolStripMenuItem.Click
        NewToolStripButton.Enabled = False
        SaveToolStripButton.Enabled = False
        PrintPreviewToolStripButton.Enabled = False
        Dim pageselector As Dialog4 = New Dialog4

        If pageselector.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
            'set toolstripstatuslabel to show whats happening
            SetTSSlabel("Loading rankings from web..")
            ' Create a new instance of the child form.
            Dim ChildForm As Form2 = shownewchild()
            ' Make it a child of this MDI form before showing it.

            'Application.Run(ChildForm)
            ChildForm.Text = "Online Ranking at: " & Date.Today
            ChildForm.load_web(pageselector.getfirstnumber, pageselector.getsecondnumber)
        End If



    End Sub

    Private Function shownewchild() As Form
        ' Create a new instance of the child form.
        Dim ChildForm As New Form2
        ' Make it a child of this MDI form before showing it.
        ChildForm.MdiParent = Me
        ' ChildForm.Text = "Window " & m_ChildFormNumber
        ChildForm.Show()
        Return ChildForm
    End Function

    Private Sub OpenFile(ByVal sender As Object, ByVal e As EventArgs) Handles OpenToolStripMenuItem.Click, OpenToolStripButton.Click
        SetTSSlabel("Load File")
        Dim OpenFileDialog As New OpenFileDialog
        OpenFileDialog.Title = "Load Csv file"
        OpenFileDialog.InitialDirectory = localdir
        OpenFileDialog.Filter = "Csv files|*.csv"

        If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
            SetTSSlabel("Loading File")
            Dim FileName As String = OpenFileDialog.FileName

            Dim temp As Form2 = shownewchild()

            temp.load_file(FileName)

        End If

        SetTSSlabel("Ready")
    End Sub

    Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SaveAsToolStripMenuItem.Click
        savetable()
    End Sub


    Private Sub ExitToolsStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub CutToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CutToolStripMenuItem.Click
        ' Use My.Computer.Clipboard to insert the selected text or images into the clipboard
    End Sub

    Private Sub CopyToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CopyToolStripMenuItem.Click
        ' Use My.Computer.Clipboard to insert the selected text or images into the clipboard
    End Sub

    Private Sub PasteToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles PasteToolStripMenuItem.Click
        'Use My.Computer.Clipboard.GetText() or My.Computer.Clipboard.GetData to retrieve information from the clipboard.
    End Sub

    Private Sub ToolBarToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ToolBarToolStripMenuItem.Click
        Me.ToolStrip.Visible = Me.ToolBarToolStripMenuItem.Checked
    End Sub

    Private Sub StatusBarToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles StatusBarToolStripMenuItem.Click
        Me.StatusStrip.Visible = Me.StatusBarToolStripMenuItem.Checked
    End Sub

    Private Sub CascadeToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CascadeToolStripMenuItem.Click
        Me.LayoutMdi(MdiLayout.Cascade)
    End Sub

    Private Sub TileVerticalToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles TileVerticalToolStripMenuItem.Click
        Me.LayoutMdi(MdiLayout.TileVertical)
    End Sub

    Private Sub TileHorizontalToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles TileHorizontalToolStripMenuItem.Click
        Me.LayoutMdi(MdiLayout.TileHorizontal)
    End Sub

    Private Sub ArrangeIconsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ArrangeIconsToolStripMenuItem.Click
        Me.LayoutMdi(MdiLayout.ArrangeIcons)
    End Sub

    Private Sub CloseAllToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CloseAllToolStripMenuItem.Click
        ' Close all child forms of the parent.
        For Each ChildForm As Form In Me.MdiChildren
            ChildForm.Close()
        Next
    End Sub

    Public Sub SetTSSlabel(ByVal status As String)
        ToolStripStatusLabel.Text = status

    End Sub


    Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click
        savetable()
    End Sub

    Private Sub savetable()
        Dim activechild As Form2 = Me.ActiveMdiChild

        If activechild IsNot Nothing Then

            Dim SaveFileDialog As New SaveFileDialog

            With SaveFileDialog
                .Title = "Save CSV file"
                .InitialDirectory = localdir
                'SaveFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
                .Filter = "Csv files|*.csv"
                .FileName = "data_" + Date.Today + ".csv"
            End With

            If (SaveFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then

                Me.ActiveMdiChild.Invoke(Sub() activechild.saveTable2CSV(SaveFileDialog.FileName))

            End If

        End If

    End Sub

    Private Sub PrintPreviewToolStripButton_Click(sender As System.Object, e As System.EventArgs) Handles PrintPreviewToolStripButton.Click
        If Me.MdiChildren.Length > 0 Then
            Dim searchform As Form2 = Me.ActiveMdiChild
            searchform.searchDataTable()
        End If
       
    End Sub

    Private Sub CheckForUpdatesToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CheckForUpdatesToolStripMenuItem.Click
        checkForUpdates()


    End Sub

    Private Sub MDIParent1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        UpdateManager.Instance.CleanUp()
    End Sub



    Private Sub MDIParent1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim updmanager As UpdateManager = UpdateManager.Instance
        updmanager.UpdateSource = New Sources.SimpleWebSource("https://dl.dropbox.com/u/97597963/sparky/AppCast.xml")
        updmanager.ReinstateIfRestarted()
        'If updmanager.State = UpdateManager.UpdateProcessState.AfterRestart Then
        updmanager.CleanUp()

        createfolder()

        'End If

    End Sub

    Private Sub checkForUpdates()
        Dim updmanager As UpdateManager = UpdateManager.Instance

        If updatechecked Then
            MsgBox("Already checked for updates")
            updmanager.CleanUp()
            Exit Sub
        End If
        If updmanager.State = Not UpdateManager.UpdateProcessState.NotChecked Then
            MsgBox("Update process already running" & vbCrLf & _
                   "Current state: " & updmanager.State.ToString)

        End If

        updmanager.BeginCheckForUpdates(AddressOf updatesChecked, Nothing)

    End Sub

    Private Sub updatesChecked(asyncresult As IAsyncResult)
        Dim updmanager As UpdateManager = UpdateManager.Instance
        updatechecked = True

        If updmanager.UpdatesAvailable > 0 Then

            Me.Invoke(Sub() updatesAvailable())

        Else
            MsgBox("No updates available at this time." & vbCrLf & _
                   "All is up to date!")

        End If

    End Sub
    Private Sub updatesAvailable()
        Dim updmanager As UpdateManager = UpdateManager.Instance


        updwindow.WebBrowser1.Navigate("https://dl.dropbox.com/u/97597963/releasenotes.html")
        AddHandler updwindow.WebBrowser1.DocumentCompleted, Sub() updatesready()


    End Sub
    Private Sub OnPrepareUpdatesCompleted(asyncResult As IAsyncResult)
        Dim updmanager As UpdateManager = UpdateManager.Instance

        If MsgBox(updmanager.UpdatesAvailable.ToString & _
                  " Updates are ready to install." & vbCrLf & _
                  "Install them now?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then

            updmanager.ApplyUpdates(True)

        End If

    End Sub

    Private Sub updatesready()
        Dim updmanager As UpdateManager = UpdateManager.Instance

        If updwindow.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then

            updmanager.BeginPrepareUpdates(AddressOf OnPrepareUpdatesCompleted, Nothing)

        End If
    End Sub

    Private Sub createfolder()
        If (Not System.IO.Directory.Exists(localdir)) Then

            Try
                System.IO.Directory.CreateDirectory(localdir)
            Catch ex As Exception
                MsgBox("Failed to create directory" & vbCrLf & ex.ToString)
            End Try


        End If
    End Sub

End Class
